import React, { Component } from "react";
import { Route, Redirect } from "react-router-dom";
import {getToken} from './utils/auth';
interface PrivateRouteProps {
    component: React.ComponentType<any>;
    path: string;
    exact?: boolean;
}

const PrivateRoute: React.FC<PrivateRouteProps> = ({
    component: Component,
    path,
    exact = false
}) => {
    const token = getToken();
    return (
        <Route
            exact={exact}
            path={path}
            render={(props: any) =>
                token? (
                    <Component {...props} />
                ) : (
                    <Redirect to={{ pathname: '/login', state: { from: props.location } }} />
                )
            }
        />
    );
};

export default PrivateRoute;

